<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>web worker测试</title>  
		
	</head>
<body>
	<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<br /><br />

<script>
function synchronizedCode() {
	var last = new Date().getTime();
	var count = 0;
	while (true) {
		var now = new Date().getTime();
		if (now - last > 1000 * 2) {
			last = now;
			count++;
			console.log('the %dth count.',count);
		}
		if (count > 4) {
			console.log('exist while.');
			break;
		}
	}
}
var w;

function startWorker()
{
	if(typeof(Worker)!=="undefined")
	{
		if(typeof(w)=="undefined")
		{
			w=new Worker("demo_workers.js");
		}
		w.onmessage = function (event) {
			console.log(event.data);
		};	
		w.postMessage(1);
		synchronizedCode();
	}
	else
	{
		document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
	}
	
}

function stopWorker()
{
	w.terminate();
}
</script>

</body>
</html>